﻿namespace System.Web.Mvc
{
    public class ImageResult : ActionResult
    {
        public ImageResult() { }

        public string ImageFile { get; set; }
        public byte[] Image { get; set; }
        public string ImageFormat { get; set; }

        public override void ExecuteResult(ControllerContext context)
        {
            // verify properties 
            if (Image == null && ImageFile == null)
            {
                throw new ArgumentNullException("Image");
            }
            if (ImageFormat == null)
            {
                throw new ArgumentNullException("ImageFormat");
            }

            // output 
            context.HttpContext.Response.Clear();
            context.HttpContext.Response.ContentType = ImageFormat;

            if (this.ImageFile == null)
                context.HttpContext.Response.BinaryWrite(this.Image);
            else
                context.HttpContext.Response.WriteFile(this.ImageFile, false);
        }
    }


}
